<?php


namespace app\system\controller;

class Config extends AdminBase
{
	public function get_website_info()
	{
		$info = \app\common\server\ConfigServer::get("website");
		$this->response(\app\system\model\Code::success, "获取成功", $info);
	}
	public function set_website_info()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["title" => "require", "keywords" => "require", "description" => "require", "favicon" => "require", "logo" => "require", "bottom_copyright" => "require", "filing_number" => "require", "qq" => "require", "mail" => "require", "url" => "require"], ["title.require" => "请输入网站标题！", "keywords.require" => "请输入keywords！", "description.require" => "请输入description！", "favicon.require" => "请选择网站favicon图标！", "logo.require" => "请选择网站logo图标！", "bottom_copyright.require" => "请输入底部版权！", "filing_number.require" => "请输入备案号！", "qq.require" => "请输入 qq", "mail.require" => "请输入 mail", "url.require" => "请输入 url"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("website", $k, $v);
		}
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function get_pay_config()
	{
		$config = \app\common\server\ConfigServer::get("pay");
		$data = ["config" => ["alipay" => ["open" => $config["config"]["alipay"]["open"] ?? true, "gateway" => $config["config"]["alipay"]["gateway"] ?? 0], "wxpay" => ["open" => $config["config"]["wxpay"]["open"] ?? true, "gateway" => $config["config"]["wxpay"]["gateway"] ?? 0], "qqpay" => ["open" => $config["config"]["qqpay"]["open"] ?? true, "gateway" => $config["config"]["qqpay"]["gateway"] ?? 0]], "alipay" => ["appId" => $config["alipay"]["appId"] ?? "", "gatewayUrl" => $config["alipay"]["gatewayUrl"] ?? "", "rsaPublicKey" => $config["alipay"]["rsaPublicKey"] ?? "", "rsaPrivateKey" => $config["alipay"]["rsaPrivateKey"] ?? "", "alipayrsaPublicKey" => $config["alipay"]["alipayrsaPublicKey"] ?? "", "InterfaceContentEncryptionMethod" => $config["alipay"]["InterfaceContentEncryptionMethod"] ?? "", "mobile_product" => $config["alipay"]["mobile_product"] ?? "", "pc_product" => $config["alipay"]["pc_product"] ?? ""], "wxpay" => ["appid" => $config["wxpay"]["appid"] ?? "", "app_secret" => $config["wxpay"]["app_secret"] ?? "", "merchant_id" => $config["wxpay"]["merchant_id"] ?? "", "mch_serial_no" => $config["wxpay"]["mch_serial_no"] ?? "", "apikey" => $config["wxpay"]["apikey"] ?? "", "apiv3key" => $config["wxpay"]["apiv3key"] ?? "", "merchantPrivateKey" => $config["wxpay"]["merchantPrivateKey"] ?? "", "platformCertificate" => $config["wxpay"]["platformCertificate"] ?? "", "pc_product" => $config["wxpay"]["pc_product"] ?? "", "mobile_product" => $config["wxpay"]["mobile_product"] ?? "", "platformCertificateId" => $config["wxpay"]["platformCertificateId"] ?? ""], "qqpay" => ["merchant_id" => $config["qqpay"]["merchant_id"] ?? "", "apikey" => $config["qqpay"]["apikey"] ?? ""], "epay_alipay" => ["url" => $config["epay_alipay"]["url"] ?? "", "id" => $config["epay_alipay"]["id"] ?? "", "key" => $config["epay_alipay"]["key"] ?? ""], "epay_wxpay" => ["url" => $config["epay_wxpay"]["url"] ?? "", "id" => $config["epay_wxpay"]["id"] ?? "", "key" => $config["epay_wxpay"]["key"] ?? ""], "epay_qqpay" => ["url" => $config["epay_qqpay"]["url"] ?? "", "id" => $config["epay_qqpay"]["id"] ?? "", "key" => $config["epay_qqpay"]["key"] ?? ""], "ysf_wxpay" => ["mchId" => $config["ysf_wxpay"]["mchId"] ?? "", "key" => $config["ysf_wxpay"]["key"] ?? ""], "ysf_alipay" => ["mchId" => $config["ysf_alipay"]["mchId"] ?? "", "key" => $config["ysf_alipay"]["key"] ?? ""], "jrm_alipay" => ["url" => $config["jrm_alipay"]["url"] ?? "", "id" => $config["jrm_alipay"]["id"] ?? "", "key" => $config["jrm_alipay"]["key"] ?? ""], "jrm_wxpay" => ["url" => $config["jrm_wxpay"]["url"] ?? "", "id" => $config["jrm_wxpay"]["id"] ?? "", "key" => $config["jrm_wxpay"]["key"] ?? ""], "jrm_qqpay" => ["url" => $config["jrm_qqpay"]["url"] ?? "", "id" => $config["jrm_qqpay"]["id"] ?? "", "key" => $config["jrm_qqpay"]["key"] ?? ""], "hhh_alipay" => ["url" => $config["hhh_alipay"]["url"] ?? "", "key" => $config["hhh_alipay"]["key"] ?? ""], "hhh_wxpay" => ["url" => $config["hhh_wxpay"]["url"] ?? "", "key" => $config["hhh_wxpay"]["key"] ?? ""], "hhh_qqpay" => ["url" => $config["hhh_qqpay"]["url"] ?? "", "key" => $config["hhh_qqpay"]["key"] ?? ""]];
		$this->response(\app\system\model\Code::success, "获取成功", $data);
	}
	public function set_pay_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["config" => "require", "alipay" => "require", "wxpay" => "require", "qqpay" => "require"], ["config.require" => "请配置 config", "alipay.require" => "请配置 alipay", "wxpay.require" => "请配置 wxpay", "qqpay.require" => "请配置 qqpay"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("pay", $k, $v);
		}
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function get_partner_platformCertificate()
	{
		$result = (new \app\api\wxpay\Certificate(\app\common\server\ConfigServer::get("pay", "wxpay")))->get_certificate();
		if ($result["code"] == 1) {
			$this->response(\app\system\model\Code::success, "获取成功", $result["data"]);
		} else {
			$this->response(\app\system\model\Code::fail, "获取失败" . $result["msg"]);
		}
	}
	public function get_options_config()
	{
		$config = \app\common\server\ConfigServer::get("options");
		$data = ["agent_subordinate_limit_level" => $config["agent_subordinate_limit_level"] ?? 0, "agent_recharge_money_limit_minimum" => $config["agent_recharge_money_limit_minimum"] ?? 0];
		$this->response(\app\system\model\Code::success, "获取成功", $data);
	}
	public function set_options_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["config" => "require|array"], ["config.require" => "请配置 config"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		foreach ($post["config"] as $k => $v) {
			\app\common\server\ConfigServer::set("options", $k, $v);
		}
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function get_sms_config()
	{
		$config = \app\common\server\ConfigServer::get("sms");
		$data = ["channel_type" => $config["channel_type"] ?? 0, "aliyun" => ["accessKeyId" => $config["aliyun"]["accessKeyId"] ?? "", "accessSecret" => $config["aliyun"]["accessSecret"] ?? "", "signName" => $config["aliyun"]["signName"] ?? "", "template_code" => ["verification_code" => $config["aliyun"]["template_code"]["verification_code"] ?? "", "card_buy_deliver" => $config["aliyun"]["template_code"]["card_buy_deliver"] ?? ""]], "tencentcloud" => ["SecretId" => $config["tencentcloud"]["SecretId"] ?? "", "SecretKey" => $config["tencentcloud"]["SecretKey"] ?? "", "appId" => $config["tencentcloud"]["appId"] ?? "", "signName" => $config["tencentcloud"]["signName"] ?? "", "template_code" => ["verification_code" => $config["tencentcloud"]["template_code"]["verification_code"] ?? "", "card_buy_deliver" => $config["tencentcloud"]["template_code"]["card_buy_deliver"] ?? ""]], "smsbao" => ["userName" => $config["smsbao"]["userName"] ?? "", "apiKey" => $config["smsbao"]["apiKey"] ?? "", "signName" => $config["smsbao"]["signName"] ?? ""]];
		$this->response(\app\system\model\Code::success, "获取成功", $data);
	}
	public function set_sms_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["channel_type" => "require"], ["channel_type.require" => "请配置 channel_type"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("sms", $k, $v);
		}
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function sms_send_test()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["phone" => "require"], ["phone.require" => "请输入测试手机号"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		list($channel_type, $config) = get_sms_config();
		$res = \app\common\Sms::get_verification_code($channel_type, $config, $post["phone"], ["other" => "systemTest"]);
		if ($res["code"] == 10000) {
			$this->response(\app\system\model\Code::success, $res["msg"]);
		} else {
			$this->response(\app\system\model\Code::fail, $res["msg"]);
		}
	}
	public function get_swoole_config()
	{
		$info = \app\common\server\ConfigServer::get("swoole");
		$data = ["tcp_port" => $info["tcp_port"] ?? 9501, "udp_port" => $info["udp_port"] ?? 9502, "http_port" => \think\facade\Request::port()];
		return json(["code" => \app\system\model\Code::success, "msg" => "获取成功", "data" => $data]);
	}
	public function set_swoole_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["tcp_port" => "require", "udp_port" => "require"], ["tcp_port.require" => "请输入 tcp_port", "udp_port.require" => "请输入 udp_port"]);
		if (!$validate->check($post)) {
			return json(["code" => \app\system\model\Code::fail, "msg" => $validate->getError()]);
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("swoole", $k, $v);
		}
		return json(["code" => \app\system\model\Code::success, "msg" => "设置成功"]);
	}
	public function get_cross_domain_config()
	{
		$info = \app\common\server\ConfigServer::get("cross_domain");
		$data = ["open" => $info["open"] ?? 0, "origin_rule" => $info["origin_rule"] ?? 0, "allow_origin" => $info["allow_origin"] ?? []];
		return json(["code" => \app\system\model\Code::success, "msg" => "获取成功", "data" => $data]);
	}
	public function set_cross_domain_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["open" => "require", "origin_rule" => "require", "allow_origin" => "require|array"], ["open.require" => "请输入 open", "origin_rule.require" => "请输入 open", "allow_origin.require" => "请输入 allow_origin"]);
		if (!$validate->check($post)) {
			return json(["code" => \app\system\model\Code::fail, "msg" => $validate->getError()]);
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("cross_domain", $k, $v);
		}
		return json(["code" => \app\system\model\Code::success, "msg" => "设置成功"]);
	}
	public function get_file_storage_config()
	{
		$config = \app\common\server\ConfigServer::get("file_storage");
		$data = ["channel_type" => $config["channel_type"] ?? 0, "aliyun" => ["accessKeyId" => $config["aliyun"]["accessKeyId"] ?? "", "accessSecret" => $config["aliyun"]["accessSecret"] ?? "", "bucket" => $config["aliyun"]["bucket"] ?? "", "endpoint" => $config["aliyun"]["endpoint"] ?? ""]];
		$this->response(\app\system\model\Code::success, "获取成功", $data);
	}
	public function set_file_storage_config()
	{
		$post = $this->request->post();
		$validate = \think\Validate::make(["channel_type" => "require"], ["channel_type.require" => "请配置 channel_type"]);
		if (!$validate->check($post)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		foreach ($post as $k => $v) {
			\app\common\server\ConfigServer::set("file_storage", $k, $v);
		}
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function get_home_info()
	{
		$template = \app\system\service\ConfigService::get_home_template_config_data();
		$template_list = [];
		$dirs = getDirList("./home/");
		foreach ($dirs as $dir) {
			$configFile = "./home/{$dir}/config.json";
			if (is_file($configFile)) {
				$template_config = json_decode(file_get_contents($configFile), true) ?? [];
				if (isset($template_config["name"]) && isset($template_config["describe"]) && isset($template_config["config"])) {
					$template_list[] = ["name" => $template_config["name"] ?? $dir, "template" => $dir];
				}
			}
		}
		$data = ["home" => ["template" => $template["template"], "config" => $template["config"]], "template_list" => $template_list];
		$this->response(\app\system\model\Code::success, "获取成功", $data);
	}
	public function set_home_config()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["config" => "require"], ["config.require" => "config 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$home_config = \app\common\server\ConfigServer::get("website", "template_config", []);
		$home_config["template"] = $options["config"]["template"];
		$home_config["config"][$options["config"]["template"]] = $options["config"]["config"];
		foreach ($home_config["config"] as $template_index => $template) {
			foreach ($template["config"] as $template_config_index => $template_config) {
				$home_config["config"][$template_index]["config"][$template_config_index] = [];
				$home_config["config"][$template_index]["config"][$template_config_index]["currentIsShow"] = $template_config["currentIsShow"];
				foreach ($template_config["config"] as $template_config_item_index => $template_config_item) {
					$home_config["config"][$template_index]["config"][$template_config_index]["config"][$template_config_item_index]["value"] = $template_config_item["value"];
				}
			}
		}
		\app\common\server\ConfigServer::set("website", "template_config", $home_config);
		$this->response(\app\system\model\Code::success, "设置成功");
	}
	public function get_home_template_config_data()
	{
		$options = $this->request->get();
		$validate = \think\Validate::make(["template" => "require"], ["template.require" => "template 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$this->response(\app\system\model\Code::success, "获取成功", \app\system\service\ConfigService::get_home_template_config_data($options["template"]));
	}
	public function clear_home_config()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["template" => "require"], ["template.require" => "template 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ConfigService::clear_home_config($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "清空成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}